const _wx = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPicker:false,
    jects:[],
    jectsValue:'',
    project_id:'',
    fieldValue:[],
    reason:''
  },
  addnewApplys(){
    wx.navigateTo({
      url: '/pages/addnewApplys/addnewApplys?fieldValue=' + JSON.stringify(this.data.fieldValue) ,
    })
  },
  onConfirm(event) {
    const { picker, value, index } = event.detail;
    console.log(value,index);
    this.setData({
      jectsValue:value.text,
      project_id:value.value,
      showPicker:false
    })
  },
  notification(){
    this.setData({showPicker:true})
  },
  onCancel() {
    this.setData({showPicker:false })
  },
  formSubmit(e) {
    var that = this
    if(!that.data.project_id){
      wx.showToast({title: '请选择项目',icon: 'none',duration: 2000});
      return
    }
    if(that.data.fieldValue.length <= 0){
      wx.showToast({title: '请选择工具',icon: 'none',duration: 2000});
      return
    }
    if(!that.data.reason){
      wx.showToast({title: '请输入申请原因',icon: 'none',duration: 2000});
      return
    }
    _wx.getData('/admin/warehouse/warehouseapply/insert', 'POST', {
      detail:that.data.fieldValue.map((e) => {
        return {
          warehouse_id: e.id,
          lib_id: e.val,
          number: e.number,
        };
      }),
      project_id:that.data.project_id.toString(),
      reason:that.data.reason,
    }, function (res) {
      if(res.code == 0){
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 2000
        });
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          })   
        }, 2000);
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {    
    var that = this
    _wx.getData('/admin/warehouse/noneedauth/projecting', 'GET', {
    }, function (res) {
      if(res.code == 0){
        var jects = res.data.map((item) => {
          console.log(item.name, item.id);
          return {
            text: item.name,
            value: item.id,
          };
        });
        console.log(jects);
        that.setData({
          jects:jects
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.data.fieldValue);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})